package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsCatalogOfWork;
import com.kma.summer2012.model.CrumbsRequestAndWork;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import java.util.*;

public class CrumbsCatalogOfWorkDAOImpl implements CrumbsCatalogOfWorkDAO {
    @Override
    public void add(CrumbsCatalogOfWork work) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(work);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsCatalogOfWork work) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(work);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsCatalogOfWork getById(int work_id) {
        Session session = null;
        CrumbsCatalogOfWork work = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            if (work_id != 0) {
                work = (CrumbsCatalogOfWork) session.get(CrumbsCatalogOfWork.class, work_id);
                if (work.getCrumbsCatalogOfTools() != null) {
                    Hibernate.initialize(work.getCrumbsCatalogOfTools());
                }
                if (work.getCrumbsCatalogOfWork() != null) {
                    Hibernate.initialize(work.getCrumbsCatalogOfWork());
                }
            }
            // if (work.getCrumbsRequestAndWorks() != null) {
            //     Hibernate.initialize(work.getCrumbsRequestAndWorks());
            // }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception in Dao was caught! ");
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return work;
    }

    @Override
    public Collection<CrumbsCatalogOfWork> getAll() {
        Session session = null;
        List<CrumbsCatalogOfWork> works = new ArrayList<CrumbsCatalogOfWork>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            works = session.createCriteria(CrumbsCatalogOfWork.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            for (CrumbsCatalogOfWork work : works) {
                if (work.getCrumbsCatalogOfTools() != null) {
                    Hibernate.initialize(work.getCrumbsCatalogOfTools());
                }
                if (work.getCrumbsCatalogOfWork() != null) {
                    Hibernate.initialize(work.getCrumbsCatalogOfWork());
                }
                //  else {work.setCrumbsCatalogOfWork(new CrumbsCatalogOfWork());}

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return works;
    }

    @Override
    public void delete(CrumbsCatalogOfWork work) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            //
            work = (CrumbsCatalogOfWork) session.get(CrumbsCatalogOfWork.class, work.getId());
            if (work != null) {
                if (work.getCrumbsCatalogOfWork() != null) {
                    CrumbsCatalogOfWork parent = (CrumbsCatalogOfWork) session.get(CrumbsCatalogOfWork.class, work.getCrumbsCatalogOfWork().getId());
                    parent.getCrumbsCatalogOfWorks().remove(work);
                    session.update(parent);
                }
                if (work.getCrumbsRequestAndWorks() != null) {
                    for (CrumbsRequestAndWork raw : work.getCrumbsRequestAndWorks()) {
                        raw = (CrumbsRequestAndWork) session.get(CrumbsRequestAndWork.class, raw.getId());
                        work.getCrumbsRequestAndWorks().remove(raw);


                    }
                    session.update(work);
                }
                session.delete(work);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void deleteAll(ArrayList<CrumbsCatalogOfWork> workList) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            for (CrumbsCatalogOfWork work : workList) {
                session.delete(work);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }

    }

    @Override
    public Set<CrumbsCatalogOfWork> getSons(CrumbsCatalogOfWork work) {
        Session session = null;
        Set<CrumbsCatalogOfWork> sons = new HashSet<CrumbsCatalogOfWork>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Hibernate.initialize(work.getCrumbsCatalogOfWorks());
            if (work.getCrumbsCatalogOfWorks() != null) {
                for (CrumbsCatalogOfWork son : work.getCrumbsCatalogOfWorks()) {
                    if (!son.isDelete()) {
                        sons.add(son);
                    }
                }
            }
            sons = work.getCrumbsCatalogOfWorks();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return sons;
    }

}
